package com.zombie.logic.object;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.zombie.ResourceManager;
import com.zombie.effect.MovingImageEffect;
import com.zombie.logic.core.ZombieWorld;
import com.zombie.logic.enums.ObjectType;
import com.zombie.util.Utils;

public class StaticObject extends GameObject {

	public int life = 10;
	
	public StaticObject(float x, float y) {
		super(x, y);
		type = ObjectType.STATIC;
	}

	@Override
	public void update(float delta) {
		super.update(delta);
		angle = MathUtils.radDeg*body.getAngle();
		if (life <= 0)
			remove();
	}

	public void remove() {
		toParts(0.04f,ResourceManager.getImage(image));
		super.remove();
		ResourceManager.getSound(getMaterial().dieSound).play();
	}

	protected void toParts(float velocity,Sprite img){
		for(int i = 0;i<8;i++){
			int width = Utils.randomInt(4,(int) (img.getRegionWidth()/4));
			int height = Utils.randomInt(4,(int) (img.getRegionHeight()/4));
			int x = Utils.randomInt(2, (int) (img.getRegionWidth()-width-2));
			int y = Utils.randomInt(2, (int) (img.getRegionHeight()-height-2));
			MovingImageEffect eff = new MovingImageEffect();
			eff.setFullLifeTime(60000);
			eff.velocity = velocity + 0.1f*Utils.rnd.nextFloat();
			eff.aVelocity = 1.4f + 0.4f*Utils.rnd.nextFloat();
			if (Utils.rnd.nextBoolean())
				eff.aVelocity = -eff.aVelocity;
			eff.stopTime = 50+ Utils.rnd.nextInt(150);
			float x1 = position.x + Utils.randomInt(-6,12);
			float y1 = position.y + Utils.randomInt(-6,12);
			TextureRegion region = new TextureRegion(img.getTexture());
			region.setRegion(img,x,y, width,height);
			eff.image =	region;
			eff.setBounds(x1, y1, width*img.getScaleX(), height*img.getScaleY());
			eff.angle =	Utils.rnd.nextFloat()*360f;
			eff.angle2 = eff.angle;
			ZombieWorld.addEffect(eff);
		}
	}

	public void checkLife() {
		life = (int) (size.x*size.y/10);
	}

}
